import * as types from '../constants/ActionTypes';
import Request from '../utils/request';

const fetchProfile = (isLoadMore, isLoading) => (dispatch) => {
  dispatch(fetchProfileList(isLoadMore, isLoading));

  Request.get({
    url: '/dealer/info',
    success: (data) => {
      dispatch(receiveProfileList(data.dealer, data.saler));
    },
    fail: () => {
      dispatch(receiveProfileList({}, {}));
    },
    error: () => {
      dispatch(receiveProfileList({}, {}));
    },
  });
};

const modifySaler = (saler, submitName, submitValue) => (dispatch) => {
  Request.post({
    url: '/dealer/staff',
    data: {
      [submitName]: submitValue,
      from: 'dealerstaffconfig',
    },
    success: () => {
      const newSaler = saler;
      newSaler[submitName] = submitValue;
      dispatch(receiveModifySaler(newSaler, ''));
    },
    fail: () => {
      dispatch(receiveModifySaler(saler, '修改失败，请重试！'));
    },
    error: () => {
      dispatch(receiveModifySaler(saler, '服务器开小猜了，请稍后再试!'));
    },
  });
};

const modifyDealer = (dealer, submitName, submitValue) => (dispatch) => {
  Request.post({
    url: '/dealer/info',
    data: {
      [submitName]: submitValue,
    },
    success: () => {
      const newDealer = dealer;
      newDealer[submitName] = submitValue;
      dispatch(receiveModifyDealer(newDealer, ''));
    },
    fail: () => {
      dispatch(receiveModifyDealer(dealer, '修改失败，请重试！'));
    },
    error: () => {
      dispatch(receiveModifyDealer(dealer, '服务器开小差了，请稍后再试!'));
    },
  });
};

const changeAvatar = (newImage, callback) => (dispatch) => {
  Request.post({
    url: '/dealer/staff/avatar',
    data: {
      file: newImage,
    },
    success: (data) => {
      console.log('上传头像成功=', data);
      // const newSaler = saler;
      // newSaler.avatar = data.initialPreviewConfig[0].file;
      // dispatch(doChangeAvatar(newSaler, ''));
      if (callback) {
        callback();
      }
    },
    fail: () => {
      dispatch(doChangeAvatar('', '头像上传失败，请重试！'));
    },
    error: () => {
      dispatch(doChangeAvatar('', '服务器开小猜了，请稍后再试!'));
    },
  });
};


const fetchProfileList = (isLoadMore, isLoading) => ({
  type: types.FETCH_PROFILE,
  isLoading,
  isLoadMore,
});

const receiveProfileList = (dealer, saler) => ({
  type: types.RECEIVE_PROFILE,
  dealer,
  saler,
});

const receiveModifySaler = (saler, errmsg) => ({
  type: types.MODIFY_SALER,
  saler,
  errmsg,
});
const receiveModifyDealer = (dealer, errmsg) => ({
  type: types.MODIFY_DEALER,
  dealer,
  errmsg,
});

const doChangeAvatar = (saler, errmsg) => ({
  type: types.CHANGE_AVATAR,
  saler,
  errmsg,
});

const changeAvatarStore = avatorStore => ({
  type: types.CHANGE_AVATAR_STORE,
  avatorStore,
});

// 个人资料
const fetchPerson = perTime => ({
  type: types.CHANGE_PER_STORE,
  perTime,
});
const fetchPersonIntro = perIntro => ({
  type: types.CHANGE_PER_STORE_INTRO,
  perIntro,
});
const fetchPersonSex = perSex => ({
  type: types.CHANGE_PER_STORE_SEX,
  perSex,
});

export {
  fetchProfile,
  modifySaler,
  modifyDealer,
  changeAvatar,
  changeAvatarStore,
  fetchPerson,
  fetchPersonIntro,
  fetchPersonSex,
};
